package com.lanmao.blog.service.impl;

import com.lanmao.blog.entity.User;
import com.lanmao.blog.mapper.UserMapper;
import com.lanmao.blog.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanmao.blog.utils.HttpUtils;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lanmao
 * @since 2022-03-13
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Override
    public User update(User user) {
        if(user.getPassword().equals("111111")){//未更新密码
            User user1 = new User();
            user1.setId(user.getId());
            user1.setAvatar(user.getAvatar());
            user1.setEmail(user.getEmail());
            user1.setNickname(user.getNickname());
            user1.setPhone(user.getPhone());
            user1.setUpdateTime(LocalDateTime.now());
            user1.setUsername(user.getUsername());
            baseMapper.updateById(user1);
        }else{//更新密码
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();//盐值加密
            stringRedisTemplate.opsForValue().set("pass",user.getPassword());
            String encode = passwordEncoder.encode(user.getPassword());
            user.setPassword(encode);
            baseMapper.updateById(user);

        }
        User user1 = baseMapper.selectById(user.getId());
        user1.setPassword("");
        return user1;
    }
    /**
     * 重要提示如下:
     * HttpUtils请从
     * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
     * 下载
     *
     * 相应的依赖请参照
     * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
     */
    /**
     * 发送验证码
     * @param phone
     * @return
     */
    @Override
    public boolean sendcode(String phone, String code) {
        String host = "https://intlsms.market.alicloudapi.com";
        String path = "/comms/sms/sendmsgall";
        String method = "POST";
        String appcode = "95decc133ee847bfbc4a4bbc13b50fbc";
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + appcode);
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("callbackUrl", "http://test.dev.esandcloud.com");
        bodys.put("channel", "0");
        bodys.put("mobile", "+86"+phone);
        bodys.put("templateID", "0000000");
        bodys.put("templateParamSet", code+", 5");
        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }




}
